<#include "/template/_ListPageWithTree.html" />

<#assign left_title="模块树"> 

<@left>
<ul id="lefttree" url="${root!}/framemenu/leftTree"></ul>
<script type="text/javascript">
			$(function(){
				$('#lefttree').tree({
					onClick:function(node){
						$('#auth-role').datagrid({
							url:'${root!}/frameauthorize/authRoleTable?code='+node.attributes.menuCode
						});
					}
				});
			});
</script>
</@left> 

<@center>

<div class="easyui-tabs" fit="true" >
	<div title="角色授权" >
		<div class="easyui-layout" fit="true">
			<div region="north" style="height: 30px;">
				 <a
				href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true"
				onclick="saveRoleAuth();">保存设置</a>
			</div>
			
			<div region="center">
				<table id="auth-role" toolbar="#tb">
					<thead>
						<tr>
							<th data-options="field:'menuName'">模块名称</th> 
							<#list roles as a>
							  <th data-options="field:'${a.id}',formatter:check,align:'center'">${a.roleName}
							  	<input type="checkbox" id="${a.id}" class="checkAll">
							  </th>
							</#list>
							<th data-options="field:'OpenAll',formatter:checkAll,align:'center'">完全公开
								<input type="checkbox" id="OpenAll" class="checkAll">
							</th> 
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</div>
	<div title="部门授权" ></div>
	<div title="个人授权" ></div>
</div>

<script>
$(function(){
	$('#auth-role').datagrid({  
	    url:'${root!}/frameauthorize/authRoleTable',
	    rownumbers:true,
	    pagination:true,
	    pageSize : ${tableSize!},
	    onDblClickRow:function(){
	    	editRoleType();
		},
		fit:true,
		checkbox:true,
		singleSelect:true,
		selectOnCheck:false
	});

	$(".checkAll").each(function(){
		$(this).off().on('click',function(){
			var id = $(this).attr("id");
			var check = this.checked;
			$('.'+id).each(function(){
				this.checked = check;
			});
		});
	});
});

function check(val,row){
	var select =false;
	var role = "";
	if(val.indexOf("@")==0){
		select = true;
		role = val.substring(1,val.length);		
	}else{
		role = val;
	}
	return '<input class="'+role+'" type="checkbox" '+(select?'checked':'')+' value="'+role+':'+row.id+'" name="menu-role"></input>';
}

function checkAll(val,row){
	return '<input name="OpenAll" class="OpenAll" type="checkbox" '+(val?'checked':'')+' value="'+row.id+'"></input>';
}

function saveRoleAuth(){
	var str="";
	var list = document.getElementsByName("menu-role");
	for(var i=0;i<list.length;i++){
		if(list[i].checked){
			str+=list[i].value+",";
		}
	}

	var str2="";
	var list = document.getElementsByName("OpenAll");
	for(var i=0;i<list.length;i++){
		if(list[i].checked){
			str2+=list[i].value+",";
		}
	}
	var data = $('#auth-role').datagrid('getData').rows;
	var ids = "";
	for(var i in data){
		ids+=data[i].id+",";
	}

	$.ajax({
		url:"${root!}/frameauthorize/saveRoleAuth",
		type:"POST",
		data:{
			roles:str,
			openall:str2,
			idlist:ids
		},
		success:function(msg){
			Alert.info(msg,"系统消息");
		}
	});
}
</script>
</@center>